//
//  LabelViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/8.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "LabelViewController.h"
#import "NSHandle.h"
#import "AllCaseModel.h"
#import "LabelCollectionViewCell.h"
#import "CaseViewController.h"
@interface LabelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *mainCollectionView;
@end

@implementation LabelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = self.label;
    
    //1创建layout对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //2配置属性
    //(2.1设置行间距(每个item上下距离)
    layout.minimumLineSpacing = 10;
    //(2.2设置左右间距(列间距
    layout.minimumInteritemSpacing =5;//>=50
    //(2.3 设置item的size
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 20,230);
    //(2.4设置距屏幕边缘的距离
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //3创建 UICollectionView
    self.mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout];
    //4添加到父视图
    [self.view addSubview:self.mainCollectionView];
    //5遵守协议 实现方法
    //设置代理
    self.mainCollectionView.backgroundColor =[UIColor whiteColor];
    
    
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    [[NSHandle shareHandle]getLabelWithStrUrl:[NSString stringWithFormat:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=1&sort[key]=default&sort[order]=desc&commodity_type=1&mark=%@&per_page=20&page=1",self.mark] WithHTTPMethod:nil WithHTTPBody:nil WithBlock:^(BOOL Finished) {
        if (Finished) {
            [self.mainCollectionView reloadData];
        }
    }];
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[NSHandle shareHandle].labelArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib = [UINib nibWithNibName:@"LabelCollectionViewCell" bundle:[NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"LabelCollectionViewCell"];
    LabelCollectionViewCell *cell = [[LabelCollectionViewCell alloc]init];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LabelCollectionViewCell" forIndexPath:indexPath];
    AllCaseModel *model = [NSHandle shareHandle].labelArray[indexPath.row];
    [cell setCellWithModel:model];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CaseViewController *caseVC = [[CaseViewController alloc]init];
    AllCaseModel *model = [NSHandle shareHandle].labelArray[indexPath.row];
   
    caseVC.ids = model.ids;
    [self.navigationController pushViewController:caseVC animated:YES];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
